package l8p4;

/**
 * <p>Title: RectangleGUI</p>
 * <p>Description: RectangleGUI
 *       Shows how to get started with displaying rectangles in a window.</p>
 * @author Anthony Varghese
 * @date   October 31st 2012
 * @version 1.0
 */


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import shapes.DrawableRectangle;
import shapes.Shape;

public class RectangleGUI extends JFrame implements ActionListener {
	/**
	 * Data members
	 * serialVersionUID is for serialization - ignore this for now.
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// Position and size of the window frame
	private static final int FRAME_X_POSITION = 200, FRAME_Y_POSITION = 100,
							 FRAME_WIDTH = 400, FRAME_HEIGHT = 300;

	private JLabel text; // Tells user what to do
	private String DESCRIPTION = "Let's draw rectangles.";
	// Position and size of the description label
	private static final int LABEL_TOP_LEFT_X = 30, // pixels from left edge of window
							 LABEL_TOP_LEFT_Y = 0, // pixels from top edge of window
							 LABEL_WIDTH = FRAME_WIDTH - 2*30, // pixels wide
							 LABEL_HEIGHT = 30;   // pixels high

	// shape refers to a Shape object
	private Shape shape;

	private JButton drawButton; // Allows us to draw shapes in a list
	// Position and size of the delete button
	private static final int BUTTON_TOP_LEFT_X = LABEL_TOP_LEFT_X + 200, // pixels from left edge of window
							 BUTTON_TOP_LEFT_Y = LABEL_TOP_LEFT_Y, // pixels from top edge of window
							 BUTTON_WIDTH = 90, // pixels wide
							 BUTTON_HEIGHT = 35;   // pixels high
	
	/**
	 * RectangleGUI default constructor
	 * 
	 */
	public RectangleGUI(){
		super("Graphical User Interface for a Rectangle");
		setUpWindow();
	}

	/**
	 * setUpWindow - add all the doo-dads we need in the window.
	 * 
	 */
	private void setUpWindow() {
		setBounds( FRAME_X_POSITION, FRAME_Y_POSITION, FRAME_WIDTH, FRAME_HEIGHT);
		// Without the next line, the program is still running even if the window is "closed"
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		// We want to position our components - labels and buttons - ourselves. So disable
		//   any default layout managers that might try to position things
		setLayout( null );
		
		text = new JLabel( DESCRIPTION );
		text.setBounds(LABEL_TOP_LEFT_X, LABEL_TOP_LEFT_Y, LABEL_WIDTH, LABEL_HEIGHT);
		add( text );
		
		
		drawButton = new JButton("Draw");
		drawButton.setBounds(BUTTON_TOP_LEFT_X, BUTTON_TOP_LEFT_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
		add( drawButton );
		drawButton.addActionListener(this);
	}

	/**
	 * actionPerformed method for MainClient
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(final ActionEvent arg0) {
		final int x = Shape.random( 0 , FRAME_WIDTH);
		final int y = Shape.random( BUTTON_TOP_LEFT_Y + BUTTON_HEIGHT , FRAME_HEIGHT);
		final int width = Shape.random( 1 , FRAME_WIDTH);
		final int height  = Shape.random( 1 , FRAME_HEIGHT -(BUTTON_TOP_LEFT_Y + BUTTON_HEIGHT) );

		shape = new DrawableRectangle( x, y, width, height );
		repaint();
	}

	/**
	 * Paint method - this is where our shapes get drawn.
	 */
	public void paint(final java.awt.Graphics graphics) {
		super.paint(graphics);
		/**
		 * We want to draw the Rectangle.
		 */
//		if (shape != null)
//			(( . . . )shape).draw( . . . );
	}
}